// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.reports.editor.design.border;

import org.dengues.commons.IDenguesColorConstants;
import org.dengues.commons.utils.ColorUtil;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.MarginBorder;
import org.eclipse.draw2d.geometry.Insets;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.graphics.Color;

/**
 * Presents border of margin
 * 
 * /** Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf Qiang.Zhang.Adolf@gmail.com 2008-2-22 qiang.zhang $
 * 
 */
public class ReportDesignMarginBorder extends MarginBorder {

    private static final Insets DEFAULT_CROP = new Insets(-3, -3, -2, -2);

    private int backgroundColor = 0xFFFFFF;

    /**
     * Constructor
     * 
     * @param insets
     */
    public ReportDesignMarginBorder(Insets insets) {
        super(insets);

    }

    /**
     * paint the border (non-Javadoc)
     * 
     * @see org.eclipse.draw2d.MarginBorder#paint(org.eclipse.draw2d.IFigure, org.eclipse.draw2d.Graphics,
     * org.eclipse.draw2d.geometry.Insets)
     */
    public void paint(IFigure figure, Graphics graphics, Insets insets) {
        Color oldBackgroundColor = graphics.getBackgroundColor();
        Color oldForegroundColor = graphics.getForegroundColor();
        Rectangle rect = figure.getBounds().getCopy();
        Insets margin = getInsets(figure);

        graphics.setBackgroundColor(ColorUtil.getColorII(backgroundColor));

        Rectangle top = new Rectangle(rect.x, rect.y, rect.width, margin.top);
        graphics.fillRectangle(top);
        Rectangle left = new Rectangle(rect.x, rect.y, margin.left, rect.height);
        graphics.fillRectangle(left);
        Rectangle bottom = new Rectangle(rect.x, rect.height - margin.bottom, rect.width, margin.bottom);
        graphics.fillRectangle(bottom);
        Rectangle right = new Rectangle(rect.width - margin.right, rect.y, margin.right, rect.height);
        graphics.fillRectangle(right);

        graphics.setBackgroundColor(oldBackgroundColor);

        graphics.setForegroundColor(IDenguesColorConstants.MarginBorderColor);
        graphics.drawRectangle(figure.getBounds().getCopy().crop(margin).crop(DEFAULT_CROP));
        graphics.setForegroundColor(oldForegroundColor);
    }

    /**
     * @param color
     */
    public void setBackgroundColor(int color) {
        if (color != 0) {
            backgroundColor = color;
        }
    }

    /**
     * @return
     */
    public int getBackgroundColor() {
        return backgroundColor;
    }

    /**
     * @param insets
     */
    public void setInset(Insets insets) {
        this.insets = insets;
    }
}